# -*- coding:utf-8 -*-
# @Time: 2021/12/10 21:24
# @Author : Saki;
# @File : test1.py
# @Software : PyCharm

from selenium.webdriver.common.keys import Keys
from django.test import TestCase
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import pandas as pd
import requests
from collections import defaultdict
import os
import django

def getlist(key_word):
    url = "https://xueshu.baidu.com/usercenter/data/authorchannel?cmd=inject_page&author="+key_word+"&affiliate="
#     driver = webdriver.PhantomJS("D:/phantomjs-2.1.1-windows/bin/phantomjs.exe")
    #     driver = webdriver.PhantomJS("D:/phantomjs-2.1.1-windows/bin/phantomjs.exe")
    driver = webdriver.Chrome("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe")
    driver.get(url)
    time.sleep(8)
    contents=[]
    content = driver.page_source.encode('utf-8')
    contents.append(content)
    urls_list = []
    for i in range(1):
        time.sleep(2)
        driver.find_element_by_class_name('c-icon-pager-next').click()
        content = driver.page_source.encode('utf-8')
        contents.append(content)
        soup = BeautifulSoup(content, 'html.parser')
        c_font = soup.find_all("a",class_="searchResult_pic")
        for c in c_font:
            url = c.attrs["href"]
            url="https://xueshu.baidu.com"+url
            urls_list.append(url)
    print(urls_list)
    driver.close()
    return urls_list

def get_item_info(url):
    #print(url)
    content_details = requests.get(url)
    soup = BeautifulSoup(content_details.text.encode("ISO-8859-1").decode("utf-8")
                         , "html.parser")
    #print(soup.encode('utf-8'))
    # 姓名
    if len(soup.select('div.p_affiliate'))!=0:
        name = ''.join(list(soup.select('div.p_affiliate')[0].stripped_strings))
        print(name)
    else:
        name='none'
        print(name)
    # 机构
    # 被引频次
    # 成果数
    # H指数
    # G指数
    # 期刊数
    # 会议数
    # 专著数
    # 其他数
    return name,



if __name__ == '__main__':
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "学术.settings")
    django.setup()
    from search.models import *
    u_list=getlist("邓")
    for url in u_list:
        get_item_info(url)

    # soup = driver_open("马克思恩格斯选集")
    # urls_list = page_url_list(soup, page=0)
    # dit = get_all_data(urls_list)






